using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

[CustomEditor(typeof(HMI_Title), true)]
public class HMI_Title_Editor : HMI_EditorBase
{
    private HMI_Title mTarget;
    private VerticalLayoutGroup mLayoutGroup;
    private MyText mText;
    private bool foldoutText = false;
    private MyTextEditor textEditor;

    private bool hasFollow; //跟随图标
    private GameObject followObj;

    public override void Awake()
    {
        base.Awake();

        mTarget = target as HMI_Title;
        mLayoutGroup = mTarget.GetComponent<VerticalLayoutGroup>();
        mText = mTarget.transform.Find("Text").GetComponent<MyText>();
        followObj = mTarget.transform.Find("Follow")?.gameObject;

        hasFollow = (followObj != null);
        mTarget.hasFollow = hasFollow;
    }

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        EditorGUI.BeginChangeCheck();
        mText.text = EditorGUILayout.TextField("文字：", mText.text);
        if (textEditor == null)
            textEditor = CreateEditor(mText) as MyTextEditor;
        foldoutText = textEditor.CreateStyleEditor(foldoutText);
        hasFollow = EditorGUILayout.Toggle("有跟随图标：", hasFollow);
        mTarget.hasFollow = hasFollow;
        if (EditorGUI.EndChangeCheck())
        {
            mTarget.SetVoiceText(mText.text);
            RefreshObj();
            EditorUtility.SetDirty(mTarget);
        }
    }

    protected override string GetDataJson()
    {
        var data = new HMI_TitleData();

        data.TextData = mText.GetData();
        data.HasFollow = hasFollow;

        string json = JsonUtility.ToJson(data, true);
        return json;
    }

    protected override void SetData(string json)
    {
        HMI_TitleData data = JsonUtility.FromJson<HMI_TitleData>(json);

        if (mText != null && data.TextData != null)
        {
            MyTextData textData = data.TextData;
            mText.SetData(textData);
        }
        hasFollow = data.HasFollow;
        mTarget.hasFollow = hasFollow;

        RefreshObj();

        base.SetData(json);
        EditorUtility.SetDirty(mTarget);
    }

    private void RefreshObj()
    {
        if (hasFollow)
        {
            if (followObj == null)
            {
                followObj = HMI_Slider_Editor.LoadMiniWidget(mTarget.transform, "Follow");

                var mFollowRect = followObj.transform as RectTransform;
                mLayoutGroup.padding.right = hasFollow ? ((int)mFollowRect.sizeDelta.x) : 0;
            }
        }
        else
        {
            var arrowGo = mTarget.transform.Find("Follow");
            if (arrowGo != null)
            {
                followObj = null;
                DestroyImmediate(arrowGo.gameObject);
            }
            mLayoutGroup.padding.right = 0;
        }
        if (hasFollow && followObj != null)
        {
            var mFollowRect = followObj.GetComponent<RectTransform>();
            if (mText.cachedTextGeneratorForLayout.lineCount > 1)
            {
                var lineInfo = mText.cachedTextGeneratorForLayout.lines[1];
                var charInfo = mText.cachedTextGeneratorForLayout.characters[lineInfo.startCharIdx - 1];
                mFollowRect.anchoredPosition = new Vector2(charInfo.cursorPos.x + charInfo.charWidth, 0);
            }
            else if (mText.cachedTextGeneratorForLayout.characters.Count > 0)
            {
                var charInfo = mText.cachedTextGeneratorForLayout.characters[mText.cachedTextGeneratorForLayout.characters.Count - 1];
                mFollowRect.anchoredPosition = new Vector2(charInfo.cursorPos.x + charInfo.charWidth, 0);
            }
        }
    }
}
[Serializable]
public class HMI_TitleData
{
    public MyTextData TextData;  //文字
    public bool HasFollow;  //是否有箭头
}
